package com.cqut.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.Permissions;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context;

import com.cqut.dto.DownloadFileDTO;
import com.cqut.util.StringUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.cqut.model.FileManager;
import com.cqut.service.IFileManagerService;
import com.cqut.dto.FileManagerDTO;
import com.cqut.dto.LimitShowDTO;
import com.cqut.util.ResponseUtil;


@RestController
@RequestMapping("/api/fileManager")
public class FileManagerController {
    @Resource(name="fileManagerService")
    private IFileManagerService fileManagerService;

    /**
     * 文件上传
     * */
    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public ResponseEntity<String>  upload(
            @RequestParam("file")
            CommonsMultipartFile file) throws IOException {
        if(file == null)
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> rs = fileManagerService.uploadFile(file);

        return ResponseUtil.getResEntityForPPPAndJson(rs);
    }

    /**
     * 文件下载
     * @throws IOException
     * */
    @RequestMapping(value="/download/{fileId}", method=RequestMethod.GET)
    public ResponseEntity<byte[]> download(
            @PathVariable("fileId")
            String fileId) throws IOException {

        if (StringUtil.isNullOrEmpty(fileId))
            return ResponseUtil.getResEntityForDownloadWhenFail();

        DownloadFileDTO dfd = fileManagerService.downloadFile(fileId);
        if(dfd == null)
            return ResponseUtil.getResEntityForDownloadWhenFail();
        return ResponseUtil.getResEntityForDownload(dfd.getFileName(), dfd.getFileType(), dfd.getFileContent());

    }

    @RequestMapping(value="/test", method=RequestMethod.GET)
    public ResponseEntity<String> test() {
        return ResponseUtil.getResEntityForGet(true);
    }
    
}
